<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="流水号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入流水号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="业务员" prop="personId">
        <el-select v-model="queryParams.personId" placeholder="请选择"  filterable    class="!w-240px">
        <el-option
            v-for="item in personList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        />
      </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择"  filterable   class="!w-240px">
        <el-option
            v-for="item in deptList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        />
      </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择"  filterable    class="!w-240px">
        <el-option
            v-for="item in warehouseList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
        />
      </el-select>
      </el-form-item>
      <el-form-item label="订单日期" prop="orderDate">
        <el-date-picker
          v-model="queryParams.orderDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item label="流程状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择流程状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['wms:rdrecord10:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['wms:rdrecord10:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" style="height: 330px;">
      <!-- 子表的列表 -->
      <!-- <el-table-column type="expand">
        <template #default="scope">
          <el-tabs model-value="rdrecords10">
            <el-tab-pane label="生产入库子" name="rdrecords10">
              <Rdrecords10List :pid="scope.row.id" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column> -->
      <el-table-column label="流水号" align="center" prop="serialNumber" width="190px"/>
      <el-table-column label="业务员" align="center" prop="personName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="仓库" align="center" prop="warehouseName" />
      <el-table-column
        label="订单日期"
        align="center"
        prop="orderDate"
        :formatter="dateFormatter2"
        width="120px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter2"
        width="120px"
      />

      <el-table-column label="流程状态" prop="status" width="100px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="160px">
        <template #default="scope">
          <el-button
            link
            type="info"
            @click="openForm('select', scope.row.id)"
            v-hasPermi="['wms:rdrecord10:update']"
          >
            查看
          </el-button>

          <el-button
          v-show="scope.row.status === 0||scope.row.status === 3||scope.row.status === 4"

            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['wms:rdrecord10:update']"
          >
            编辑
          </el-button>
          <el-button
          v-show="scope.row.status === 0||scope.row.status === 3||scope.row.status === 4"

            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['wms:rdrecord10:delete']"
          >
            删除
          </el-button>

          <el-button
          v-show="scope.row.status === 0||scope.row.status === 3||scope.row.status === 4"

            link
            type="success"
            @click="submit(scope.row.id)"
            v-hasPermi="['wms:rdrecord10:create']"
          >
            提交
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

</template>

<script setup lang="ts">
import { dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { Rdrecord10Api, Rdrecord10VO } from '@/api/wms/rdrecord10'
import Rdrecord10Form from './Rdrecord10Form.vue'
import Rdrecords10List from './components/Rdrecords10List.vue'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ref, reactive, onMounted } from 'vue'
import { defaultProps, handleTree } from '@/utils/tree';
import { WarehouseApi, WarehouseVO } from '@/api/wms/warehouse'
import { PersonApi, PersonVO } from '@/api/wms/person'
import { getSimpleDeptList } from '@/api/system/dept';
import { ProductionOrderApi, ProductionOrderVO } from '@/api/wms/productionorder'

/** 生产入库主 列表 */
defineOptions({ name: 'Rdrecord10' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { push } = useRouter() // 路由

const loading = ref(true) // 列表的加载中
const list = ref<Rdrecord10VO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  serialNumber: undefined,
  personId: undefined,
  deptId: undefined,
  warehouseId: undefined,
  orderDate: [],
  remark: undefined,
  createTime: [],
  status: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const personList = ref([] as PersonVO[]) // 岗位列表
const deptList = ref<Tree[]>([]) // 树形结构
const warehouseList = ref([] as WarehouseVO[]) // 
const productionOrderList = ref([] as ProductionOrderVO[])

//加载数据
onMounted(async () => {
    //加载人员数据
    personList.value = await PersonApi.getPersonSimpleList()
  //加载部门树
  deptList.value = handleTree(await getSimpleDeptList())
  warehouseList.value= await WarehouseApi.getWarehouseSimpleList()
  productionOrderList.value = await ProductionOrderApi.findProductionOrderOperateList()
  
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await Rdrecord10Api.getRdrecord10Page(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (type: string, id?: number) => {
  if(type=='create')
  {
    push('/order/rdrecord10FormCreate?id='+id+"&type="+type)
  }else if(type=='select'){
    push('/order/rdrecord10FormSelect?id='+id+"&type="+type)
  }else if(type=='update'){
    push('/order/rdrecord10FormUpdate?id='+id+"&type="+type)
  }
}
/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await Rdrecord10Api.deleteRdrecord10(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await Rdrecord10Api.exportRdrecord10(queryParams)
    download.excel(data, '生产入库主.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 提交按钮操作 */
const submit = async (id: number) => {
  try {
    await message.confirm('是否提交')
    await Rdrecord10Api.submit(id)
    message.success(t('common.submit'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>